﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="wNewMilestone.xaml.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Interaction logic for wNewMilestone.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

// ReSharper disable InconsistentNaming
namespace Mar3ek.HouseLight
{
    using System;
    using System.Windows;
    using LighthouseNET;
    /// <summary>
    /// Interaction logic for wNewMilestone.xaml
    /// </summary>
    public partial class wNewMilestone
    {
        private readonly int _projectId;
        private readonly LighthouseAPI _api;
        private readonly bool _editing;
        private readonly Milestone _milestone;

        /// <summary>
        /// Initializes a new instance of the <see cref="wNewMilestone"/> class.
        /// </summary>
        /// <param name="api">The API.</param>
        /// <param name="projectId">The project id.</param>
        public wNewMilestone(LighthouseAPI api, int projectId)
        {
            InitializeComponent();
            _projectId = projectId;
            _api = api;
            Result = false;
            dpDueOn.SelectedDate = DateTime.Now;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="wNewMilestone"/> class. 
        /// </summary>
        /// <param name="api">
        /// The API instance
        /// </param>
        /// <param name="milestone">
        /// The milestone.
        /// </param>
        public wNewMilestone(LighthouseAPI api, Milestone milestone)
        {
            InitializeComponent();
            _api = api;
            Title = "Editing milestone";
            _milestone = milestone;
            _editing = true;
            txtTitle.Text = milestone.Title;
            txtGoals.Text = milestone.Goals;
            Result = false;

            if (!milestone.DueOn.HasValue)
            {
                return;
            }

            chkDueOn.IsChecked = true;
            dpDueOn.SelectedDate = milestone.DueOn.Value;
        }

        /// <summary>
        /// Gets a value indicating whether an update has been sent.
        /// </summary>
        /// <value>
        ///   <c>true</c> if sent; otherwise, <c>false</c>.
        /// </value>
        internal bool Result { get; private set; }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Handles the Click event of the btnSubmit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtTitle.Text) || string.IsNullOrEmpty(txtGoals.Text))
            {
                return;
            }

            try
            {
                var milestone = new Milestone(txtTitle.Text, txtGoals.Text, chkDueOn.IsChecked == true && dpDueOn.SelectedDate != null ? dpDueOn.SelectedDate : null);
                if (_editing)
                {
                    _api.UpdateMilestone(_milestone.ProjectId, _milestone.Id, milestone);
                }
                else
                {
                    _api.CreateMilestone(_projectId, milestone);
                }

                Result = true;
                Close();
            }
            catch (LighthouseResponseException ex)
            {
                MessageBox.Show(
                    ex.Message,
                    "HouseLight",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            catch (LighthouseServerException ex)
            {
                MessageBox.Show(
                    string.Format("A communication error was encountered. Check your internet connection and try again.\nIf the problem persists, contact the plugin developer.\nError details follow:\n\n{0}", ex.Message),
                    "HouseLight",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }
    }
}

// ReSharper restore InconsistentNaming